# -*- coding: utf-8 -*-
import time
from datetime import datetime,date,timedelta
import xadmin
from xadmin import views
from .models import *
from .resources import *
from .forms import PurchaseForm, SaleForm, GoodInPurchaseForm, GoodInSaleForm


# class ××××Admin(object):
#     # xadmin plugin
#     list_display = []
#     searchfields = []  # 搜索该类的外键字段 fk__attribute student__name
#     list_filter = []
#     ordering = ["-"]  # 默认排序方式 点击字段都可正序/倒序排列
#     readonly_fields = []
#     exclude = [] # include相对，与readonly_fields冲突
#     relfield_style = "fk-ajax"  #  作为其他类引用的外键不再是直接下拉显示全部对象的__str__方法返回的字符串，而是显示含搜索内容的字段
#     list_editable = []  # 可以在列表页中直接编辑
#     inlines = []  # 引用该类的类，本类作为外键，外键为一，一对多
#     refresh_times = [3,5]  # 单位秒
# xadmin.site.register(××××,××××Admin)


class SaleAdmin(object):
    all_fields = [field.name for field in Sale._meta.fields]
    all_fields.remove("id")
    class GoodInSaleInline(object):
        model = GoodInSale
        # exclude = ['', ]
        extra = 1
        style = 'tab'
    inlines = [GoodInSaleInline, ]
    list_display = all_fields
    show_detail_fields = ["code","fk_store","fk_clerk"]  # 本身唯一标志和外键
    list_display_links = ["code"]
    list_editable = [ "fk_store","fk_clerk"]
    list_per_page = 15
    list_export = []
    import_export_args = {'import_resource_class': SaleResource,
                          'export_resource_class': SaleResource}
    form = SaleForm
    list_filter = ["code","fk_store", "fk_clerk", "created", "modified", "info"]
    search_fields = ["code","fk_store__title", "fk_clerk__username"]
    relfield_style = 'fk-ajax'  # GoodInSaleAdmin Filter


class GoodInSaleAdmin(object):
    relfield_style = 'fk-ajax'
    all_fields = [field.name for field in GoodInSale._meta.fields]
    list_filter = ["fk_sale", "fk_good", "quantity", "created", "modified", "info"]
    list_editable = ["fk_sale", "fk_good", "quantity", "created", "info"]
    list_display = all_fields
    import_export_args = {'import_resource_class': GoodInSaleResource,
                          'export_resource_class': GoodInSaleResource}
    form = GoodInSaleForm



class SaleCommentAdmin(object):
    hidden_menu = True
    list_display = [field.name for field in SaleComment._meta.fields]
    import_export_args = {'import_resource_class': SaleCommentResource,
                          'export_resource_class': SaleCommentResource}


class GoodCommentAdmin(object):
    hidden_menu = True
    list_display = [field.name for field in GoodComment._meta.fields]
    import_export_args = {'import_resource_class': GoodCommentResource,
                          'export_resource_class': GoodCommentResource}
    style_fields = {'user_permissions': 'm2m_transfer'}


class PurchaseAdmin(object):
    all_fields = [field.name for field in Purchase._meta.fields]
    all_fields.remove("id")
    class GoodInPurchaseInline(object):
        model = GoodInPurchase
        # exclude = ['', ]
        extra = 1
        style = 'tab'

    list_per_page = 50
    inlines = [GoodInPurchaseInline, ]
    list_display = all_fields
    show_detail_fields = ["code", "fk_store", "fk_clerk"]  # 本身唯一标志和外键
    list_display_links = ["code"]
    list_editable = ["fk_store", "fk_clerk"]
    list_filter = ["code","fk_store", "fk_clerk", "created", "modified", "info"]
    import_export_args = {'import_resource_class': PurchaseResource,
                          'export_resource_class': PurchaseResource}
    form = PurchaseForm
    relfield_style = 'fk-ajax'

    # 定义在model中和optionclass中函数效果是一样的，区别在optionclass中定义需要形参data接收model instance
    # def amount_each_purchase(self, data):
    #     # print(type(data))  # Purchase
    #     # print(data.__dict__)  # Purchase Instance
    #     return data.amount()


class GoodInPurchaseAdmin(object):
    list_export = []
    import_export_args = {'import_resource_class': GoodInPurchaseResource,
                          'export_resource_class': GoodInPurchaseResource}
    all_fields = [field.name for field in GoodInPurchase._meta.fields]
    list_display = all_fields  # 可以直接填外键
    list_filter = ["fk_purchase", "fk_good", "quantity", "created", "modified", "info"]
    list_editable = ["fk_purchase", "fk_good", "quantity", "created", "info"]
    relfield_style = "fk-ajax"
    form = GoodInPurchaseForm


class RefundAdmin(object):
    all_fields = [field.name for field in Refund._meta.fields]
    all_fields.remove("id")
    list_filter = all_fields
    list_display = all_fields
    list_display_links = ["code"]
    list_editable = ["fk_goodinsale", "quantity", "info"]
    import_export_args = {'import_resource_class': RefundResource,
                          'export_resource_class': RefundResource}


class ExpenseAdmin(object):
    list_editable = ["amount", "type", "fk_store"]
    all_fields = [field.name for field in Expense._meta.fields]
    all_fields.remove("id")
    list_filter = all_fields
    list_display = all_fields
    list_display_links = ["code"]
    import_export_args = {'import_resource_class': ExpenseResource,
                          'export_resource_class': ExpenseResource}


xadmin.site.register(Sale, SaleAdmin)
xadmin.site.register(GoodInSale, GoodInSaleAdmin)
xadmin.site.register(SaleComment, SaleCommentAdmin)
xadmin.site.register(GoodComment, GoodCommentAdmin)
xadmin.site.register(Purchase, PurchaseAdmin)
xadmin.site.register(GoodInPurchase, GoodInPurchaseAdmin)
xadmin.site.register(Refund, RefundAdmin)
xadmin.site.register(Expense, ExpenseAdmin)

